package com.hcrazy.manchester.app.controllers.comment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hcrazy.manchester.app.form.CommentSearchForm;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.AttachmentService;
import com.hcrazy.manchester.app.services.CommentService;
import com.hcrazy.manchester.app.services.ForumService;
import com.hcrazy.manchester.app.services.GroupService;
import com.hcrazy.manchester.app.services.LiveSortService;
import com.hcrazy.manchester.app.services.RentService;
import com.hcrazy.manchester.app.services.SecondaryService;
import com.hcrazy.manchester.app.services.TopicService;
import com.hcrazy.manchester.app.services.UserService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

/**
 * 评论部分
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/comment")
public class CommentController {
	
	@Autowired
	private CommentService commentService;

	@Autowired
	private LiveSortService liveSortService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private TopicService topicService;
	
	@Autowired
	private ForumService forumService;
	
	@Autowired
	private RentService rentService;
	
	@Autowired
	private SecondaryService secondaryService;
	
	@Autowired
	private AttachmentService attachmentService;
	
	@Autowired
	private GroupService groupService;
	
	@RequestMapping(value="/comment",method=RequestMethod.GET)
	public void comment(Model model){
		PageModel pageModel = new PageModel();
		
		try {
			pageModel = commentService.getCommonInfos(pageModel, Constant.COMM_ADDRESS_INFO_MODULE, "","");
			model.addAttribute("pageModel", pageModel);
			//初始化查询对象
			model.addAttribute("searchForm", new CommentSearchForm());
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search",method=RequestMethod.POST)
	public String search(@ModelAttribute("form") CommentSearchForm form,Model model){
		//是否执行删除操作
		if(form.getCommentId() > 0){
			try {
				commentService.delComment(form.getCommentId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置sellerId
		form.setCommentId(0);
		
		//根据查询参数获取分页对象
		PageModel m = new PageModel();
		m.setPageNum(form.getPageNum());
		try {
			PageModel pageModel = commentService.getCommonInfos(m, form.getModuleType(), form.getTitle(), form.getUsername());
			model.addAttribute("pageModel", pageModel);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/comment/comment";
	}
	
}
